package com.linln.admin.good.service.impl;

import com.linln.admin.good.domain.Category;
import com.linln.admin.good.domain.Good;
import com.linln.admin.good.repository.GoodRepository;
import com.linln.admin.good.service.GoodService;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 小懒虫
 * @date 2021/04/03
 */
@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodRepository goodRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    public Good getById(Long id) {
        return goodRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Good> getPageList(Example<Good> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return goodRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param good 实体对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Good save(Good good) {

        if(good.getId() == null) {
            Good save = goodRepository.save(good);
            //新增sku需要生成商品编码
            String s = generateCode(save.getId(), save.getCategory_id1(), save.getCategory_id2(), save.getCategory_id3(), new HashMap<>());
            save.setGood_code(s);
            //保存商品编码
            return goodRepository.save(save);
        }
        return  goodRepository.save(good);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return goodRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }


    /**
     * 生成SPU、SKU编码
     * SPU编码规则：“S”+一级分类编码(截取前5位)+二级分类编码(截取前5位)+三级分类编码(截取前5位)+八位数字+“-”+贸易类型标示位
     * SKU编码规则：一级分类编码(截取前5位)+二级分类编码(截取前5位)+三级分类编码(截取前5位)+八位数字+“-”+贸易类型标示位
     *
     * @param id
     * @param categoryId1 一级分类id
     * @param categoryId2 二级分类id
     * @param categoryId3 三级分类id
     * @param goodsCategoryMap
     * @return
     */
    private String generateCode(Long id, Long categoryId1, Long categoryId2, Long categoryId3, Map<Long, Category> goodsCategoryMap) {
        return StringUtils.join("SKU",
                StringUtils.substring(goodsCategoryMap.get(categoryId1)==null?"":goodsCategoryMap.get(categoryId1).getCategoryCode(), 0, 5),
                StringUtils.substring(goodsCategoryMap.get(categoryId2)==null?"":goodsCategoryMap.get(categoryId2).getCategoryCode(), 0, 5),
                StringUtils.substring(goodsCategoryMap.get(categoryId3)==null?"":goodsCategoryMap.get(categoryId3).getCategoryCode(), 0, 5),
                String.format("%08d", id));
    }
}